primes = [2, 3, 5, 7]

planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']

hands = [
    ['J', 'Q', 'K'],
    ['2', '2', '2'],
    ['6', 'A', 'K'],  # (Comma after the last element is optional)
]
# (I could also have written this on one line, but it can get hard to read)
hands_2 = [['J', 'Q', 'K'], ['2', '2', '2'], ['6', 'A', 'K']]

# A list can contain a mix of different types of variables:
my_favourite_things = [32, 'raindrops on roses', help]

"""
Indexing
You can access individual list elements with square brackets.
Python uses zero-based indexing, so the first element has index 0.
"""
print(planets)
print("Which planet is closest to the sun?")
print(planets[0])

# Elements at the end of the list can be accessed with negative numbers, starting from -1:
print("Which planet is furthest from the sun?")
print(planets[-1])
# reverse
print("Which planet is second furthest from the sun?")
print(planets[-2])

"""
Slicing
What are the first three planets? We can answer this question using slicing:
planets[0:3] is our way of asking for the elements of planets starting from index 0 
and continuing up to but not including index 3.
"""
print("============Slicing lists===============")

print(planets[0:3])
print(planets[3:])
# The last 3 planets
print(planets[-3:])
print(planets[-9:])  # planets[0:]

"""
Changing lists
Lists are "mutable", meaning they can be modified "in place".
One way to modify a list is to assign to an index or slice expression.
For example, let's say we want to rename Mars:
"""
print("============Changing lists===============")

planets[3] = 'Balachandran'
print(planets)
planets[:3] = ['Mur', 'Vee', 'Ur']
print(planets)
# That was silly. Let's give them back their old names
planets[:4] = ['Mercury', 'Venus', 'Earth', 'Mars', ]
print(planets)

"""
List functions
Python has several useful functions for working with lists.
"""
print("============List functions===============")

# len gives the length of a list:
help(len)
print(len(planets))

# sorted returns a sorted version of a list:
# The planets sorted in alphabetical order
help(sorted)
print(sorted(planets))

# primes_1 = [2, 3, 5, "11"]
primes_1 = [2, 3, 5, 11]
# sum does what you might expect:
print(sum(primes_1))
# get the minimum or maximum of several arguments
print(max(primes_1))

"""
Interlude: objects
I've used the term 'object' a lot so far - you may have even read that everything in Python is an object.
What does that mean?
In short, objects carry some things around with them. You access that stuff using Python's dot syntax.
For example, numbers in Python carry around an associated variable called imag representing their imaginary part.
 (You'll probably never need to use this unless you're doing some very weird math.)
"""
print("============Interlude: objects===============")

x = 12
# x is a real number, so its imaginary part is 0.
print(x.imag)
print(x.bit_count())
print(x.bit_length())
# Here's how to make a complex number, in case you've ever been curious:
c = 12 + 3j
print(c.imag)
help(x.bit_length)

"""
List methods
"""
print("===========List methods========")
# list.append modifies a list by adding an item to the end:
# Pluto is a planet darn it!
planets.append('Pluto')
help(planets.append)
help(list.append)
print(planets)
# pop()函数，弹出list最后一个元素，
print(planets.pop())
print(len(planets))
print(planets)
# 获取不存在的元素，会报错
print(planets.index('Saturn'))
# print(planets.index('Pluto'))
# Is Earth a planet?
print("Earth" in planets)
# Is Calbefraques a planet?
print("Calbefraques" in planets)
# help(in)
# help(planets)

"""
Tuples

Tuples are almost exactly the same as lists. They differ in just two ways.
1: The syntax for creating them uses parentheses instead of square brackets
2: They cannot be modified (they are immutable).

Tuples are often used for functions that have multiple return values.
For example, the as_integer_ratio() method of float objects 
returns a numerator and a denominator in the form of a tuple:
"""
print("========Tuples=========")
t1 = (1, 2, 3)
t2 = 1, 2, 3  # equivalent to above
print(t1)
print(t2)
# t1[0] = 100#编译器提示报错

x = 0.125
print(x.as_integer_ratio())
numerator, denominator = x.as_integer_ratio()
print(numerator)
print(denominator)
print(numerator / denominator)